<?php

namespace App\Datalog;

use App\Services\WanSdk\WanSdkService;
use Jenssegers\Agent\Facades\Agent;
use YQ\YqExtend;

class RequestLog extends LaravelDatalogBase
{
    // 日志名称
    protected $name = 'request';

    // 请求日志单独存放
    protected $logPath = 'datalogs/request';

    protected $maxSaveDay = 150;

    protected $formatter = 'context';

    public function addLog()
    {
        $input = request()->input();
        $params = [
            'create_time'  => time(),
            'create_time2' => microtime(true),
            'date_time'    => date('Y-m-d H:i:s', time()),
            'ip'           => YqExtend::getIP(),
            'url'          => request()->fullUrl(),
            'path'         => request()->path(),
            'method'       => request()->method(),
            'ua'           => Agent::getUserAgent(),
            'env_type'     => WanSdkService::getInstance()->getEnvType($input),
            'input_data'   => '',
            'data'         => [],
        ];
        if ($params['method'] == 'POST') {
            $params['data'] = $input;
            if (empty($params['data'])) {
                $params['input_data'] = file_get_contents('php://input');
            }
        }

        $this->log($params);
    }
}
